package com.example.smartcity_40.lawyer;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.example.smartcity_40.Outpatient.bean.StatusBean;
import com.example.smartcity_40.R;
import com.example.smartcity_40.lawyer.bean.LawyerBean;
import com.example.smartcity_40.tool.Tool;

import org.json.JSONException;
import org.json.JSONObject;

import kotlin.Unit;
import kotlin.jvm.functions.Function1;

public class EvaluatelawyerActivity extends AppCompatActivity {

    private EvaluatelawyerActivity context;
    private Tool tool;
    private String seekdetails;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_evaluatelawyer);
        context = this;
        initview();
    }

    private void initview() {
        ImageView back = findViewById(R.id.back);
        RatingBar rating = findViewById(R.id.rating);
        EditText user_content = findViewById(R.id.user_content);
        TextView save = findViewById(R.id.save);
        Intent intent = getIntent();
        seekdetails = intent.getStringExtra("seekdetails");
        tool = new Tool(context);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.finish();
            }
        });

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    String content = user_content.getText().toString();
                    int  nums =Math.round( rating.getRating());
                    Log.e("TAG","NUMS="+nums);
                    Log.e("TAG","seekdetails="+seekdetails);
                    if(!TextUtils.isEmpty(content) && nums !=0 && !TextUtils.isEmpty(seekdetails)){
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("id",seekdetails);
                        jsonObject.put("evaluate",content);
                        jsonObject.put("score",nums);
                        sendevaluete(jsonObject.toString());
                    }else {
                        Toast.makeText(context,"请填写信息",Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void sendevaluete(String JOSN) {
        tool.send("/prod-api/api/lawyer-consultation/legal-advice/evaluate",
                Tool.POST,
                JOSN,
                true,
                StatusBean.class,
                new Function1<StatusBean, Unit>() {
                    @Override
                    public Unit invoke(StatusBean statusBean) {
                        if (statusBean.getCode() == 200) {
                            Toast.makeText(context,"评价成功",Toast.LENGTH_SHORT).show();
                        }else {
                            Toast.makeText(context,statusBean.getMsg(),Toast.LENGTH_SHORT).show();
                        }
                        return null;
                    }
                });
    }
}
/*服务评价页，显示评分选项、评价本文框和提交评价按钮，评
分最多可选 5 颗星，最少可选 1 颗星，点击评价按钮完成评价
* */